package com.example.tuter.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tuter.R;
import com.example.tuter.entity.Comment;
import com.example.tuter.util.TimeUnitUtil;

import java.util.List;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentViewHolder> {
    private Context context;
    public List<Comment> data;
    private final int TYPE_ITEM = 1;
    private final int TYPE_FOOTER = 2;

    public CommentAdapter(List<Comment> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @NonNull
    @Override
    public CommentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_comment, parent, false);
            return new CommentViewHolder(view, viewType);

        } else if (viewType == TYPE_FOOTER) {
            View footview = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.no_content_more, parent, false);
            return new CommentViewHolder(footview, viewType);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull CommentViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_ITEM) {
            Comment comment = data.get(position);
            holder.commentUsername.setText(comment.getUsername());
            holder.commentContent.setText(comment.getContent());
            String time = "丢了";
            try {
                TimeUnitUtil.getTimeByTimestampString(Long.valueOf(comment.getCreateTime()));
            } catch (Exception ignored) {

            }
            holder.commentTime.setText(time);
        }
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        // 最后一个item设置为FooterView
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    public class CommentViewHolder extends RecyclerView.ViewHolder {
        private TextView commentUsername;
        private TextView commentTime;
        private TextView commentContent;
        LinearLayout footView;

        public CommentViewHolder(@NonNull View itemView, int viewType) {
            super(itemView);
            if (viewType == TYPE_ITEM) {
                commentUsername = itemView.findViewById(R.id.recomment_username);
                commentContent = itemView.findViewById(R.id.recomment_content);
                commentTime = itemView.findViewById(R.id.recomment_time);
            } else if (viewType == TYPE_FOOTER) {
                footView = (LinearLayout) itemView;
            }
        }
    }
}
